<template>
    <div>
        <el-card>
            <el-page-header content="用户列表" icon="" title="用户管理" />
            <el-table :data="tableData" style="width: 100%;height: 100%;">
                <el-table-column prop="username" label="用户名" width="180" />
                <el-table-column prop="avatar" label="头像" width="180">
                    <template #default="scope">
                        <div style="display: flex; align-items: center" v-if="scope.row.avatar">
                            <el-avatar :size="50" :src="'http://localhost:3000' + scope.row.avatar" />
                        </div>
                        <div style="display: flex; align-items: center" v-else>
                            <el-avatar :size="50"
                                src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="role" label="角色">
                    <template #default="scope">
                        <el-tag :type="scope.row.role == '1' ? 'danger' : 'success'">{{ scope.row.role == '1' ? '管理员' : '编辑'
                        }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="" label="操作">
                    <template #default="scope">
                        <el-button size="small" @click="handleEdit(scope.row)" type="primary">编辑</el-button>
                        <el-popconfirm title="你确定要删除么?" confirm-button-text="是" cancel-button-text="否"
                            @confirm="handleDelete(scope.row)">
                            <template #reference>
                                <el-button size="small" type="danger">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-dialog v-model="dialogFormVisible" title="编辑用户" width="50%">
            <el-form ref="userFormRef" :model="userForm" :rules="userFormRules" label-width="80px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="userForm.username" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="userForm.password" type="password" />
                </el-form-item>
                <el-form-item label="角色" prop="gender">
                    <el-select v-model="userForm.role" class="m-2" placeholder="Select" style="width: 100%;">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="个人简介" prop="introduction">
                    <el-input v-model="userForm.introduction" type="textarea" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="handleEditConfirm">
                        确定
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';



const userFormRef = ref()
const userForm = reactive({
    username: '',
    password: '',
    role: 2,   //1是管理员  2是编辑
    introduction: '',
})

const userFormRules = reactive({
    username: [
        { required: true, message: '请输入名字', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
    ],
    role: [
        { required: true, message: '请选择权限', trigger: 'blur' },
    ],
    introduction: [
        { required: true, message: '请输入介绍', trigger: 'blur' },
    ]
})

const options = ref([{ label: '管理员', value: 1 }, { label: '编辑', value: 2 }])

const tableData = ref([])
const dialogFormVisible = ref(false)
onMounted(() => {
    getTableData()
})

// const getTableData = async () => {
//     const res = await axios.get('/adminapi/user/list')
//     console.log(res)
//     tableData.value = res.data.data
// }

const getTableData = async () => {

    try {
        const res = await axios.get('/adminapi/user/list')
        console.log(res)
        tableData.value = res.data.data
    } catch (err) {
        console.log(err)
    }
}

const handleEdit = async (data) => {
    // console.log(data)
    const res = await axios.get(`/adminapi/user/list/${data._id}`)
    console.log(res)
    Object.assign(userForm, res.data.data[0])
    dialogFormVisible.value = true
}

const handleEditConfirm = () => {
    console.log(userForm)
    userFormRef.value.validate(async (valid) => {
        if (valid) {
            let res = await axios.put(`/adminapi/user/list/${userForm._id}`, userForm)
            console.log(res)
            if (res.data.ActionType == 'ok') {
                getTableData()
                dialogFormVisible.value = false
                ElMessage.success('更新成功')
            }
        }
    })
}

const handleDelete = async (data) => {
    console.log(data)
    const result = await axios.delete(`/adminapi/user/list/${data._id}`)
    console.log(result)
    if (result.data.ActionType == 'ok') {
        getTableData()
    }
}

const submitForm = () => {
    userFormRef.value.validate(async (valid) => {
        if (valid) {
            console.log(userForm)
        }
    })
}
</script>

<style lang="scss" scoped>
.el-table {
    margin-top: 50px;
}
</style>